#include "stdafx.h"
#include "Config.h"

#include <sstream>
#include <vector>
#include <string>

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

//namespace pktoon
/*{*/
	Config::Config(const SI_CHAR* filePath)
		: m_filePath(filePath), m_modified(false)
	{
		this->m_ini.LoadFile(filePath);
	}

	Config::~Config()
	{
		this->Flush();
	}


	void Config::GetAllSec( std::vector<string_t>& secs )
	{
		secs.clear();
		CSimpleIni::TNamesDepend names;
		this->m_ini.GetAllSections(names);
		for (CSimpleIni::TNamesDepend::iterator it(names.begin());
			it != names.end(); ++it)
		{
			secs.push_back((it)->pItem);
		}
	}

	void Config::GetAllKey( const SI_CHAR* section, std::vector<string_t>& keys )
	{
		keys.clear();
		CSimpleIni::TNamesDepend names;
		this->m_ini.GetAllKeys(section, names);
		for (CSimpleIni::TNamesDepend::iterator it(names.begin());
			it != names.end(); ++it)
		{
			keys.push_back((it)->pItem);
		}
	}

	const SI_CHAR* Config::GetString( const SI_CHAR* section, const SI_CHAR* key, const SI_CHAR* def /*= ""*/ ) const
	{
		return this->m_ini.GetValue(section, key, def);
	}

	int Config::GetInt(const SI_CHAR* section, const SI_CHAR* key, int def) const
	{
		const SI_CHAR* val = this->m_ini.GetValue(section, key);
		if (val == 0)
		{
			return def;
		}

		return _tstoi(val);
	}

	bool Config::GetBool(const SI_CHAR* section, const SI_CHAR* key, bool def) const
	{
		const SI_CHAR* val = this->m_ini.GetValue(section, key);
		if (val == 0)
		{
			return def;
		}

		return _tcscmp(val, _T("0")) != 0;
	}

	

	void Config::SetString(const SI_CHAR *section, const SI_CHAR *key, const SI_CHAR *value)
	{
		this->m_ini.SetValue(section, key, value);
		this->m_modified = true;
	}

	void Config::SetInt(const SI_CHAR* section, const SI_CHAR* key, int value)
	{
#ifdef _UNICODE
		std::wstringstream ss;
#else
		std::stringstream ss;
#endif
		ss.setf(std::ios_base::dec);
		ss << value;
		this->m_ini.SetValue(section, key, ss.str().c_str());
		this->m_modified = true;
	}

	void Config::SetBool(const SI_CHAR* section, const SI_CHAR* key, bool value)
	{
		this->m_ini.SetValue(section, key, value ? _T("1") : _T("0"));
		this->m_modified = true;
	}

	

	void Config::Flush()
	{
		if (!m_modified)
		{
			return;
		}
		FILE* fp = NULL;
		if(::_tfopen_s(&fp, this->m_filePath.c_str(), _T("wb")) != 0)
		{
			return ;
		}

		if (fp != 0)
		{
			this->m_ini.SaveFile(fp);
			::fclose(fp);
		}
		this->m_modified = false;
	}

	float Config::GetFloat( const SI_CHAR* section, const SI_CHAR* key, float def /*= 0.0f*/ ) const
	{
		const SI_CHAR* val = this->m_ini.GetValue(section, key);
		if (val == 0)
		{
			return def;
		}

		return (float)_tstof(val);
	}

	void Config::SetFloat( const SI_CHAR* section, const SI_CHAR* key, float value )
	{
#ifdef _UNICODE
		std::wstringstream ss;
#else
		std::stringstream ss;
#endif
		ss.setf(std::ios_base::dec);
		ss << value;
		this->m_ini.SetValue(section, key, ss.str().c_str());
		this->m_modified = true;		
	}

// }
